import {
  reqUser,
  reqAddUser,
  reqRemoveUser,
  reqSearchSingleUser,
  reqEditUser,
  reqNews,
  reqAddNews,
  reqSingleNews,
  reqEditNews,
  reqRemoveNews,
  reqSearchNews,
  reqTopic,
  reqAddTopic,
  reqRemoveTopic,
  reqTodayNews,
  reqSidebar,
  reqLogin,
  reqComments,
  reqSearchComments,
  reqRemoveComments,
  reqNewsComments,
  reqAddComment,
  reqSendCode,
  reqEditPassword
} from "../api/request";
import {
  RECEIVE_USER,
  RECEIVE_SINGLE_USER,
  RECEIVE_NEWS,
  RECEIVE_SINGLE_NEWS,
  RECEIVE_TOPIC,
  RECEIVE_TODAY_NEWS,
  RECEIVE_SIDEBAR,
  RECEIVE_LOGIN_INFO,
  RECEIVE_COMMENTS,
  RECEIVE_NEWS_COMMENTS,
  RECEIVE_VERIFICATION_CODE
} from "./mutation-types";

export default {
  //获取用户信息
  async getUser({commit}) {
    const result = await reqUser()
    if (result.status === 200) {
      const user = result.data
      commit(RECEIVE_USER, {user})
    }
  },
  //添加用户
  async addUser({commit}, user_info) {
    return await reqAddUser(user_info)
  },
  //删除用户
  async removeUser({commit}, user_id) {
    const result = await reqRemoveUser(user_id)
    if (result.status === 200) {
      console.log('ok')
    }
  },
  //查询单个用户信息
  async searchSingleUser({commit}, id) {
    const result = await reqSearchSingleUser(id)
    if (result.status === 200) {
      const singleUser = result.data
      commit(RECEIVE_SINGLE_USER, singleUser)
    }
  },
  //修改用户
  async editUser({commit}, user_info) {
    const result = await reqEditUser(user_info)
    if (result.status === 200) {
      console.log('ok')
    }
  },
  //获取新闻列表
  async getNews({commit}) {
    const result = await reqNews()
    if (result.status === 200) {
      const news = result.data
      commit(RECEIVE_NEWS, {news})
    }
  },
  //添加新闻
  async addNews({commit}, news) {
    const result = await reqAddNews(news)
    if (result.status === 200) {
      console.log('ok')
    }
  },
  //获取单个新闻
  async getSingleNews({commit}, id) {
    const result = await reqSingleNews(id)
    if (result.status === 200) {
      const news = result.data
      commit(RECEIVE_SINGLE_NEWS, news)
    }
  },
  //修改新闻
  async editNews({commit}, news_info) {
    const result = await reqEditNews(news_info)
    if (result.status === 200) {
      console.log('ok')
    }
  },
  //删除新闻
  async removeNews({commit}, id) {
    const result = await reqRemoveNews(id)
    if (result.status === 200) {
      console.log('ok')
    }
  },
  //搜索新闻
  async searchNews({commit}, keyword) {
    const result = await reqSearchNews(keyword)
    if (result.status === 200) {
      const news = result.data
      commit(RECEIVE_NEWS, {news})
    }
  },
  //获取主题
  async getTopic({commit}) {
    const result = await reqTopic()
    if (result.status === 200) {
      const topic = result.data
      commit(RECEIVE_TOPIC, topic)
    }
  },
  //添加主题
  async addTopic({commit}, topic) {
    const result = await reqAddTopic(topic)
    if (result.status === 200) {
      console.log('ok')
    }
  },
  //删除主题
  async removeTopic({commit}, topic) {
    const result = await reqRemoveTopic(topic)
    if (result.status === 200) {
      console.log('ok')
    }
  },
  //获取今日要闻
  async getTodayNews({commit}) {
    const result = await reqTodayNews()
    if (result.status === 200) {
      const todayNews = result.data
      commit(RECEIVE_TODAY_NEWS, todayNews)
    }
  },
  //获取侧边栏
  async getSidebar({commit}) {
    const result = await reqSidebar()
    if (result.status === 200) {
      const sidebar = result.data
      commit(RECEIVE_SIDEBAR, sidebar)
    }
  },
  //登录
  async login({commit}, user) {
    const result = await reqLogin(user)
    if (result.status === 200) {
      const user_info = result.data
      commit(RECEIVE_LOGIN_INFO, user_info)
    }
    return result
  },
  //获取所有评论
  async getComments({commit}) {
    const result = await reqComments()
    if (result.status === 200) {
      const comments = result.data
      commit(RECEIVE_COMMENTS, comments)
    }
  },
  //关键词查询评论
  async searchComments({commit}, keyword) {
    const result = await reqSearchComments(keyword)
    if (result.status === 200) {
      const comments = result.data
      commit(RECEIVE_COMMENTS, comments)
    }
  },
  //删除评论
  async removeComments({commit}, id) {
    const result = await reqRemoveComments(id)
    if (result.status === 200) {
      console.log('ok')
    }
  },
  //新闻id查询评论
  async searchNewsComment({commit}, id) {
    const result = await reqNewsComments(id)
    if (result.status === 200) {
      const newsComments = result.data
      commit(RECEIVE_NEWS_COMMENTS, newsComments)
    }
  },
  //发表评论
  async addComments({commit}, comments) {
    const result = await reqAddComment(comments)
    if (result.status === 200) {
      console.log('ok')
    }
  },
  //获取验证码
  async getSendCode({commit}, phone) {
    const result = await reqSendCode(phone)
    if (result.status === 200) {
      commit(RECEIVE_VERIFICATION_CODE, result.data.code)
    }
    return result
  },
  async editPassword({commit}, user) {
    const result = await reqEditPassword(user)
    if (result.status === 200) {
      console.log('ok')
    }
    return result
  }
}